<?php

namespace app\api\controller\v1;

use app\admin\model\Inventory;
use app\admin\model\Materialimage;
use app\admin\model\Ordercar;
use app\admin\model\Surface;
use app\admin\model\Texture;
use think\Cache;

class Collectorder extends Base
{

    public function collectList()
    {
        $cacheTag       = \config('cache_tag')['v1_Collectorder_collectList'];
        $cacheKey       = $cacheTag. $this->datacenter . $this->forgid . md5($this->user_id);
        $data           =  \cache($cacheKey);



        if(!$data)
        {
            $data = Ordercar::with(['material'=>function ($query) {$query->where('forgid','in',$this->forgids);},
                'inventoryone'])->order('id','desc')
                ->where('ordercar.fk_bdl_bd_datacenter',$this->datacenter)
                ->where('fk_bdl_userid',$this->user_id)
                ->select()->each(function($a){

                    $a->show_mes     = $this->getInventoryStatusMessage($a->inventoryone['fk_bdl_usableqty']);

                    $a->textureone      = Texture::where('id',$a->inventoryone['fk_bdl_bd_texture'])
                        ->find();
                    $a->surfaceone      = Surface::where('id',$a->inventoryone['fk_bdl_bd_surface'])
                        ->find();

                    //表面
                    $surface_id     = $a->surfaceone['id'];
                    $material_id    = $a['material']['id'];
                    if(!empty($surface_id) && !empty($material_id))
                    {
                        $find       = Materialimage::where('fk_bdl_bd_surface',$surface_id)->where('fk_bdl_bd_material',$material_id)->find();
                        if(!empty($find))
                        {
                            $a['show_image'] = $find['fk_bdl_imageurl'];
                        }else{
                            $a['show_image'] = $this->getDefaultImage();
                        }
                    }else{
                        $a['show_image'] = $this->getDefaultImage();
                    }


                    $a->show_image   = $a->show_image ? $a->show_image : $this->getDefaultImage();
                    //拿到长宽厚
                    $show = [];
                    foreach ($a->inventoryone->inventoryentryvalue as $key => $val)
                    {
                        if(in_array($val['fnumber'],['bdl_thickness','bdl_width','bdl_length','bdl_refthickness','bdl_materialgrade']))
                        {
                            $show[$val['fnumber']] = $val['pivot']['fk_bdl_matpropvalue'];
                        }
                    }

                    $a->show_matprop = $show;
                });

            \cache($cacheKey,$data,$this->cacheExpire);

        }


        $this->success('',$data);
    }
    public function addCollect()
    {
        $check      = $this->plulicValidate($this->params,
            ['fk_bdl_bd_material|产品ID', 'fk_bdl_inventoryid|库存ID'],
            ['require',  'require',  'require']);

        if ($check['code'] == 1)
        {
            $this->error($check['msg']);
        }

        $data = [
            'fk_bdl_bd_material'            => $this->params['fk_bdl_bd_material'],
            'fk_bdl_inventoryid'            => $this->params['fk_bdl_inventoryid'],
            'fk_bdl_userid'                 => $this->user_id,
            'forgid'                        => $this->forgid,
            'fk_bdl_bd_datacenter'          => $this->datacenter
        ];

        //拿到库存量
        $invenCount     = Inventory::get($this->params['fk_bdl_inventoryid']);
        //先查找有没有是否有数据
        $isExits        = Ordercar::where($data)->find();

        if($isExits)
        {
            $count = $isExits['fk_bdl_qty'] + 1;

            if($count > $invenCount['fk_bdl_qty'])
            {
                $this->error('输入库存数大于可用库存数');
            }

            $res   = Ordercar::where('id',$isExits['id'])->update([
                'fk_bdl_qty'     => $count
            ]);
        }else{

            //默认1件
            $data['fk_bdl_qty']  = 1;
            $res   = Ordercar::create($data);


        }

        $cacheTag       = \config('cache_tag')['v1_Collectorder_collectList'];
        $cacheKey       = $cacheTag. $this->datacenter . $this->forgid . md5($this->user_id);
        Cache::rm($cacheKey);

        if($res)
        {
            $this->success('添加成功');
        }
        $this->error('添加失败');

    }

    public function editCountCollect()
    {
        $check      = $this->plulicValidate($this->params,
            ['id|采购单ID', 'fk_bdl_qty|数量'],
            ['require', 'require']);

        if ($check['code'] == 1)
        {
            $this->error($check['msg']);
        }
        //判断是否有这么多库存
        $coll               = Ordercar::get($this->params['id']);
        $get_count          = Inventory::get($coll['fk_bdl_inventoryid']);

        if($get_count['fk_bdl_qty'] < $this->params['fk_bdl_qty'])
        {
            $this->error('输入库存数大于可用库存数');
        }


        $res = Ordercar::where('id',$this->params['id'])->update([
            'fk_bdl_qty'            => $this->params['fk_bdl_qty']
        ]);

        $cacheTag       = \config('cache_tag')['v1_Collectorder_collectList'];
        $cacheKey       = $cacheTag. $this->datacenter . $this->forgid . md5($this->user_id);
        Cache::rm($cacheKey);

        if($res)
        {
            $this->success('操作成功');
        }

        $this->error('操作失败');
    }

    public function delCollect()
    {
        $check      = $this->plulicValidate($this->params,
            ['ids|采购单ID'],
            ['require']);

        if ($check['code'] == 1)
        {
            $this->error($check['msg']);
        }

        $count  = 0;
        $res    = Ordercar::where('id','in',$this->params['ids'])->select();

        foreach ($res as $item) {
            $count += $item->delete();
        }

        $cacheTag       = \config('cache_tag')['v1_Collectorder_collectList'];
        $cacheKey       = $cacheTag. $this->datacenter . $this->forgid . md5($this->user_id);
        Cache::rm($cacheKey);
        if($count)
        {
            $this->success('删除成功');
        }

        $this->error('删除失败');

    }
}